<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
/**
 * Prunus Almendro
 *
 * Contenedor Almendro
 *
 * @author		agosto
 * @date		  20090531
 * @version		0.1
 * @copyright	Copyright (c) 2009, 7dedos
 * @link		http://www.7dedos.com.ar
 */
class prunus_almendro
{
	protected $ci;
  protected $buffer;
  protected $offsetPosition;
  protected $actions;
  protected $hiddenFields;
  protected $blocks;
  protected $title;
  protected $helpTrigger;
  protected $updateURI;
  protected $errorMsg;
  protected $cancelLabel;
  protected $saveLabel;
  protected $loadingGifPath;
  protected $successFunction;
  protected $lastLineWidth; //usado para trackear el ancho del ultimo bloque abierto
  protected $formId;
  protected $formAction;
  protected $formMultipart;
  public $puedeEditar;
  public $draggable;
    
	public function __construct()	{
		$this->ci =& get_instance();
    $this->ci->load->helper('form');
    $this->ci->load->config('prunus_almendro');
    $this->offsetPosition = $this->ci->config->item('PRAL_defaultOffset');
    $this->puedeEditar = FALSE;
    $this->lastLineWidth = 0;
    $this->draggable = FALSE;
    
		log_message('debug', 'prunus_almendro inicializado');
	}
	
  private function bufferClear() { $this->buffer = ''; }
  private function bufferAdd($position, $text) { $this->buffer .= $this->ci->prunuslib->gi ( ($this->offsetPosition + $position), $text); }
  
  public function open ($title = "", $helpTrigger = "", $offset = -1) {
    if ($offset != -1) $this->$offsetPosition = $offset;
    $this->title = $title;
    $this->helpTrigger = $helpTrigger;
    $this->bufferClear();
  } //open
  
  public function configAjax ($updateURI,$errorMsg,$cancelLabel,$saveLabel,$loadingGifPath,$successFunction) {
    $this->updateURI = $updateURI;
    $this->errorMsg = $errorMsg;
    $this->cancelLabel = $cancelLabel;
    $this->saveLabel = $saveLabel;
    $this->loadingGifPath = $loadingGifPath;
    $this->successFunction = $successFunction;
  }

  public function configForm ($id, $actionURI, $multiPart = FALSE) {
    $this->formId = $id;
    $this->formAction = $actionURI;
    $this->formMultipart = $multiPart;
  }

  public function openBlock100 ($title) {
    $this->bufferClear();
    if ($this->lastLineWidth < 100 && $this->lastLineWidth > 0) $this->bufferAdd(2, "</div>"); //si no se llego a 100, fuerzo el cierre de la linea
    if ($this->lastLineWidth == 0) $this->bufferAdd(2, "<div class=\"PRAL_blocksLine\">");
    $this->bufferAdd(4, "<div class=\"PRAL_block100\">");
    $this->bufferAdd(6, form_fieldset($title));
    $this->bufferAdd(6, "<dl>");
    $this->lastLineWidth = 100;
  } //openBlock100
  
  public function openBlock50 ($title) {
    $this->bufferClear();
    if ($this->lastLineWidth == 0) $this->bufferAdd(2, "<div class=\"PRAL_blocksLine\">");
    $this->bufferAdd(4, "<div class=\"PRAL_block50\">");
    $this->bufferAdd(6, form_fieldset($title));
    $this->bufferAdd(6, "<dl>");
    $this->lastLineWidth += 50;
  } //openBlock50

  public function addBlockItem ($id, $title, $data, $editable = FALSE) {
    $this->bufferAdd(8, "<dt>" . $title . "</dt>");
    if ($id != '') $id = "id=\"" . $id . "\"";
    if ($editable) {
      $this->bufferAdd(8, "<dd " . $id . " class=\"pr_update_textbox\">" . $data . "&nbsp;</dd>");
    } else {
      $this->bufferAdd(8, "<dd " . $id . ">" . $data . "&nbsp;</dd>");
    }
  } //addBlockItem
  
  public function closeBlock () {
    $this->bufferAdd(6, "</dl>");
    $this->bufferAdd(6, form_fieldset_close());
    $this->bufferAdd(4, "</div>");
    if ($this->lastLineWidth>=99) { 
      $this->bufferAdd(2, "</div>"); //blocksLine
      $this->lastLineWidth = 0;
    }
    $this->blocks[] = $this->buffer;
  } //closeBlock

  public function addDraggableBlock50($title, $resultItems, $containerClass, $itemClass, $itemIdField, $itemLabelField, $itemIdPrefix, $containerId) {
    $this->draggable = TRUE;
    $this->bufferClear();
    if ($this->lastLineWidth == 0) $this->bufferAdd(2, "<div class=\"PRAL_blocksLine\">");
    $this->bufferAdd(4, "<div class=\"PRAL_block50\">");
    $this->bufferAdd(6, form_fieldset($title,array('id'=>$containerId,'class'=>$containerClass)));
    $this->lastLineWidth += 50;

    foreach($resultItems as $item) {
      foreach($item as $key => $value) {
        if ($key == $itemIdField) $id = $value;
        if ($key == $itemLabelField) $label = $value;
      }
      $this->bufferAdd(8, "<div class=\"PRAL_draggableItem " . $itemClass . "\" id=\"" . $itemIdPrefix . $id . "\">" . $label . "</div>");
    }
    $this->bufferAdd(6, form_fieldset_close());
    $this->bufferAdd(4, "</div>");
  
    if ($this->lastLineWidth>=99) { 
      $this->bufferAdd(2, "</div>"); //blocksLine
      $this->lastLineWidth = 0;
    }
    $this->blocks[] = $this->buffer;
  } //addDraggableBlock50

  public function addFreeBlock50 ($id, $title, $text ,$actions = '') {
    $this->bufferClear();
    if ($this->lastLineWidth == 0) $this->bufferAdd(2, "<div class=\"PRAL_blocksLine\">");
    $this->bufferAdd(4, "<div class=\"PRAL_block50\">");
    $this->bufferAdd(6, form_fieldset($title));
    if ($actions !== '') {
      $this->bufferAdd(6, "<div class=\"PRAL_block_actions\">");
      $this->bufferAdd(6, $actions);
      $this->bufferAdd(6, "</div>");
    }
    $this->bufferAdd(6, "<div class=\"PRAL_freeBlock50\" id=\"" . $id . "\">" . $text . "</div>");
    $this->lastLineWidth += 50;
    $this->bufferAdd(6, form_fieldset_close());
    $this->bufferAdd(4, "</div>");
    if ($this->lastLineWidth>=99) { 
      $this->bufferAdd(2, "</div>"); //blocksLine
      $this->lastLineWidth = 0;
    }
    $this->blocks[] = $this->buffer;
  } //addFreeBlock50

  public function addHelpBlock50 ($title, $text) {
    $this->bufferClear();
    if ($this->lastLineWidth == 0) $this->bufferAdd(2, "<div class=\"PRAL_blocksLine\">");
    $this->bufferAdd(4, "<div class=\"PRAL_block50\">");
    $this->bufferAdd(6, form_fieldset($title));
    $this->bufferAdd(6, "<p class=\"PRAL_helpBlock50\">" . $text . "</p>");
    $this->lastLineWidth += 50;
    $this->bufferAdd(6, form_fieldset_close());
    $this->bufferAdd(4, "</div>");
    if ($this->lastLineWidth>=99) { 
      $this->bufferAdd(2, "</div>"); //blocksLine
      $this->lastLineWidth = 0;
    }
    $this->blocks[] = $this->buffer;
  } //addHelpBlock50
  public function addHelpBlock100 ($title, $text) {
    $this->bufferClear();
    if ($this->lastLineWidth < 100 && $this->lastLineWidth > 0) $this->bufferAdd(2, "</div>"); //si no se llego a 100, fuerzo el cierre de la linea
    if ($this->lastLineWidth == 0) $this->bufferAdd(2, "<div class=\"PRAL_blocksLine\">");
    $this->bufferAdd(4, "<div class=\"PRAL_block100\">");
    $this->bufferAdd(6, form_fieldset($title));
    $this->bufferAdd(6, "<p class=\"PRAL_helpBlock100\">" . $text . "</p>");
    $this->bufferAdd(6, form_fieldset_close());
    $this->bufferAdd(4, "</div>");
    $this->bufferAdd(2, "</div>"); //blocksLine
    $this->lastLineWidth = 0;
    $this->blocks[] = $this->buffer;
  } //openBlock100

  public function addHiddenField ($name,$value) {
    $this->hiddenFields[] = array($name,$value);
  } //addHiddenField
  
  public function addAction ($uri,$text,$onclick='') {
    $this->actions[] = array($uri,$text,$onclick);
  } //addAction
  
  public function renderScripts() {
    $this->bufferClear();
    
    if ($this->puedeEditar) {
      $this->bufferAdd(0, "<script type=\"text/javascript\">");
      $this->bufferAdd(0, "//<![CDATA[");
      $this->bufferAdd(0, "$(document).ready(function(){");
      $this->bufferAdd(2, "$(\".pr_update_textbox\").editInPlace({");
      $this->bufferAdd(4, "url: \"" . $this->updateURI . "\",");
      $this->bufferAdd(4, "bg_over: \"#fff\",");
      $this->bufferAdd(4, "field_type: \"text\",");
      $this->bufferAdd(4, "show_buttons: true,");
      $this->bufferAdd(4, "default_text: '',");
      $this->bufferAdd(4, "error: function() { alert('" . $this->errorMsg . "'); },");
      if ($this->successFunction != '') $this->bufferAdd(4, "success: function() { " . $this->successFunction . " },");
      $this->bufferAdd(4, "cancel_button: '<button class=\"inplace_cancel\">" . $this->cancelLabel . "<\/button>',");
      $this->bufferAdd(4, "save_button: '<button class=\"inplace_save\">" . $this->saveLabel . "<\/button>',");
      $this->bufferAdd(4, "saving_image: \"" . $this->loadingGifPath . "\" });");
      $this->bufferAdd(0, "});");
      $this->bufferAdd(0, "//]]>");
      $this->bufferAdd(0, "</script>");
    }
    return $this->buffer;
  }
  
  public function render() {
    $this->bufferClear();
    $this->bufferAdd(0, "<div class=\"PRAL_container\">");
    
    if ($this->formAction != '') {
      if ($this->formMultipart) {
        $this->bufferAdd(0, form_open_multipart($this->formAction,array('id' => $this->formId)));
      } else {
        $this->bufferAdd(0, form_open($this->formAction,array('id' => $this->formId)));
      }
      if (count($this->hiddenFields) > 0) {
        foreach ($this->hiddenFields as $field) {
          $this->bufferAdd(2, "<p><input type=\"hidden\" id=\"" . $field[0] . "\" name=\"" . $field[0] . "\" value=\"" . $field[1] . "\" /></p>");
        }
      }
    } 

    if ($this->title != '' || $this->helpTrigger !='') {
      $this->bufferAdd(2, "<div class=\"PRAL_title\">");
      if ($this->title != '') $this->bufferAdd(4, "<span class=\"title\">" . $this->title . "</span>");
      if ($this->helpTrigger != '') $this->bufferAdd(4, $this->helpTrigger);
      $this->bufferAdd(2, "</div>");
    }
    foreach($this->blocks as $block) $this->bufferAdd($this->offsetPosition * -1,$block);
    if ($this->lastLineWidth < 100 && $this->lastLineWidth > 0) $this->bufferAdd(2, "</div>"); //si no se llego a 100, fuerzo el cierre de la linea
  
    
    if (count($this->actions) > 0) {
      $this->bufferAdd(2, "<h2>" . lang('pr_Actions') . "</h2>");
      $this->bufferAdd(2, "<div id=\"pageActions\">");
      foreach ($this->actions as $action) {
        if ($action[2] == '') {
          $this->bufferAdd(4, "<span class=\"pageAction\">" . anchor($action[0],$action[1]) . "</span>");
        } else {
          $this->bufferAdd(4, "<span class=\"pageAction\">" . anchor('#',$action[1],array("onclick"=> $action[2] )) . "</span>");
        }
      }
      $this->bufferAdd(2, "</div>");
    }

    if ($this->formAction != '') {
      $this->bufferAdd(0, form_close());
    }
    
    $this->bufferAdd(0, "</div>");
    
    return $this->buffer;
  } //render
  
}
